package de.dis2011.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class land
{
    private int landid = -1;
    private String landname;

    public int getLandid()
    {
        return landid;
    }

    public void setLandid(int landid)
    {
        this.landid = landid;
    }

    public String getLandname()
    {
        return landname;
    }

    public void setLandname(String landname)
    {
        this.landname = landname;
    }

    /**
     * Lädt eine Information aus der Datenbank
     * 
     * @param id
     * 
     * @return Informations-Instanz
     */
    public static land load(int id)
    {
        try
        {
            // Hole Verbindung
            Connection con = DB2ConnectionManager.getInstance().getConnection();

            // Erzeuge Anfrage
            String selectSQL = "SELECT * FROM landid WHERE landid = ?";
            PreparedStatement pstmt = con.prepareStatement(selectSQL);
            pstmt.setInt(1, id);

            // Führe Anfrage aus
            ResultSet rs = pstmt.executeQuery();
            if (rs.next())
            {
                land ts = new land();
                ts.setLandid(rs.getInt("landid"));
                ts.setLandname(rs.getString("name"));
                System.out.println("ID: " + ts.getLandid() + " Name: " + ts.getLandname());
            }
            rs.close();
            pstmt.close();

        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public static land loadAll()
    {
        try
        {
            // Hole Verbindung
            Connection con = DB2ConnectionManager.getInstance().getConnection();

            // Erzeuge Anfrage
            String selectSQL = "SELECT * FROM landid";
            PreparedStatement pstmt = con.prepareStatement(selectSQL);

            // Führe Anfrage aus
            ResultSet rs = pstmt.executeQuery();
            while (rs.next())
            {
                land ts = new land();
                ts.setLandid(rs.getInt("landid"));
                ts.setLandname(rs.getString("name"));
                System.out.println("ID: " + ts.getLandid() + " Name: " + ts.getLandname());
            }
            rs.close();
            pstmt.close();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return null;
    }

}
